<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <meta name="format-detection" content="telephone=no">
    <title>签到</title>
    <link rel="stylesheet" href="../css/public.css">
    <link rel="stylesheet" href="../css/signin.css">
    <link rel="stylesheet" href="/js/plugins/dialog/dialog.css">
    <link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css" />
    <script src="../js/jquery/jquery.min.js"></script>
    <script src="/js/plugins/dialog/dialog.min.js"></script>
    <script src="/js/plugins/jquery-form/jquery.form.js"></script>
    <script src="/js/plugins/jrender/jrender.js"></script>
</head>
<body>
<div class="nav-search">
    <a href="/mine/profiles.html">
        <span><i class="fa fa-angle-left fa-2x" ></i></span>
    </a>
</div>
<div class="top flex flex-align-end flex-pack-center flex-warp">
    <div class="out-1 flex flex-align-center flex-pack-center" id="signIn">
        <div class="out-2 flex flex-align-center flex-pack-center" >
            <form id="siginFrom" action="/signins" method="post">
                <input type="hidden" id="userId" name="userId"/>
                <input type="hidden" id="time" name="time"/>
                <div class="signBtn">
                    <strong id="sign-txt">签到</strong>
                    <span>连续<em id="sign-count"></em>天</span>
                </div>
            </form>
        </div>
    </div>
</div>
<div class="tips" id="tips"></div>
<div class="Calendar">
    <div id="toyear" class="flex flex-pack-center">
        <div id="idCalendarPre">&lt;</div>
        <div class="year-month">
            <span id="idCalendarYear">2018</span>年<span id="idCalendarMonth">6</span>月
        </div>
        <div id="idCalendarNext">&gt;</div>
    </div>
    <table border="1px" cellpadding="0" cellspacing="0">
        <thead>
        <tr class="tou">
            <td>日</td>
            <td>一</td>
            <td>二</td>
            <td>三</td>
            <td>四</td>
            <td>五</td>
            <td>六</td>
        </tr>
        </thead>
        <tbody id="idCalendar">
        </tbody>
    </table>
</div>
<script src="../js/rili.js"></script>
<script language="JavaScript">
    var user = JSON.parse(sessionStorage.getItem("user"));
    function getTime() {
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth()+1;
        var day = date.getDate();
        if (month >= 1 && month <= 9){
            month = "0" + month;
        }
        if (day >=1 && day <= 9){
            day = "0" + day;
        }
        return year+"-"+month+"-"+day;
    }
    var isSign = false;
    var myday = new Array(); //已签到的数组

    var cale = new Calendar("idCalendar", {
        qdDay: myday,
        onToday: function(o) {
            o.className = "onToday";
        },
        onSignIn: function (){
            $$("sign-txt").innerHTML = '已签到';
        },
        onFinish: function() {
            $$("idCalendarYear").innerHTML = this.Year;
            $$("idCalendarMonth").innerHTML = this.Month; //表头年份
        }
    });
    $$("idCalendarPre").onclick = function() {
        cale.PreMonth();
    }
    $$("idCalendarNext").onclick = function() {
        cale.NextMonth();
    }
    //发送ajax请求,查询用户的历史签到记录
    $.get("/signins/"+user.id+"/signins",function (data) {
        var arr = data;
        for (var i = 0;i < arr.length;i++){
            var str = Date.parse(data[i].time);
            var history = new Date(str);
            cale.SignIn(history)
        }

        //console.log(myday);
    })

    //发送ajax请求,查询当天的签到记录
    $.get("/signins/"+user.id,function (data) {
        if (data){
            $$("sign-txt").innerHTML = '已签到';
            $.get("/signins/"+user.id+"/days",function (data) {
                $("#sign-count").html(data);
            })
            $$("tips").innerHTML = "今天已签到，获得1积分";
        }else{
            $$("sign-txt").innerHTML = '签到';
        }
    })
    //添加今天签到
    $$("signIn").onclick = function() {
        $("#siginFrom").ajaxSubmit(function (data) {
            if (data.success){
                $$("sign-txt").innerHTML = '已签到';
                isSign = true;
                cale.SignIn(new Date());
                location.reload();
            } else {
                $(document).dialog({
                    titleShow: false,
                    style: "IOS",
                    content: data.msg,
                    autoClose: 1500
                });
            }
        });
    }
    $(function () {
        $("#userId").attr("value",user.id);
        $("#time").attr("value",getTime());
    });
</script>

</body>

</html>